package com.scut.dispatching_server.tool;


import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.scut.dispatching_server.config.GlobalVariables;
import com.scut.dispatching_server.model.RLTransferOrderValueState;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/**
 * ValueFunctionPredictor
 *
 * @author yuxi
 * @date 2019-05-06
 */
public class ValueFunctionPredictor {
    public String request(String orderIds, String waitingRounds, String driverIds, String globalTimeStamp, String serverAddress) throws Exception{
        URL url = new URL(serverAddress);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        PrintWriter printWriter = new PrintWriter(conn.getOutputStream());
        printWriter.print("orderIds=" + orderIds + "&waitingRounds=" + waitingRounds + "&driverIds=" + driverIds + "&globalTimeStamp=" + globalTimeStamp);
        printWriter.flush();
        InputStream inputStream = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuilder result = new StringBuilder();
        String strRead = null;
        while((strRead = reader.readLine()) != null){
            result.append(strRead);
            result.append("\r\n");
        }
        reader.close();
        inputStream.close();
        conn.disconnect();
        return result.toString();
    }
    public ArrayList<RLTransferOrderValueState> getValueFunction(ArrayList<String> orderIds, ArrayList<String> waitingRounds, ArrayList<String> driverIds, String globalTimeStamp) throws Exception{
        StringBuilder orderIdStrings = new StringBuilder();
        StringBuilder waitingRoundStrings = new StringBuilder();
        StringBuilder driverIdStrings = new StringBuilder();
        for(int i = 0; i < orderIds.size(); i++){
            orderIdStrings.append(orderIds.get(i));
            waitingRoundStrings.append(waitingRounds.get(i));
            orderIdStrings.append(",");
            waitingRoundStrings.append(",");
        }
        for(int i = 0; i < driverIds.size(); i++){
            driverIdStrings.append(driverIds.get(i));
            driverIdStrings.append(",");
        }
        String jsonString = request(orderIdStrings.deleteCharAt(orderIdStrings.lastIndexOf(",")).toString(),
                waitingRoundStrings.deleteCharAt(waitingRoundStrings.lastIndexOf(",")).toString(),
                driverIdStrings.deleteCharAt(driverIdStrings.lastIndexOf(",")).toString(),
                globalTimeStamp,
                GlobalVariables.VALUE_FUNCTION_SERVER_ADDRESS);
        JsonParser parse = new JsonParser();
        JsonObject r = (JsonObject)parse.parse(jsonString);
        ArrayList<RLTransferOrderValueState> results = new ArrayList<>();
        for(int i = 0; i < orderIds.size(); i++){
            RLTransferOrderValueState rlTransferOrderValueState = new RLTransferOrderValueState();
            rlTransferOrderValueState.setOrderId(orderIds.get(i));
            String resultString = r.get(String.valueOf(i)).toString();
            resultString = resultString.substring(1, resultString.length() - 1);
            String[] expectedValueAndState = resultString.split(":");
            rlTransferOrderValueState.setExpectedValue(Float.parseFloat(expectedValueAndState[0]));
            rlTransferOrderValueState.setState(expectedValueAndState[1]);
            results.add(rlTransferOrderValueState);
        }
        return results;
    }
}

